package com.open.party.cloud.view.home.jiCengDangJian.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.VolunteerMemberEntity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.view.image.rounded.RoundedImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class ZyzTuanDuiMemberListAdapter extends RecyclerView.Adapter<ZyzTuanDuiMemberListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<VolunteerMemberEntity> mData;
    private Context mContext;

    public ZyzTuanDuiMemberListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setData(List<VolunteerMemberEntity> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<VolunteerMemberEntity> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ZyzTuanDuiMemberListAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.zyz_tuan_dui_member_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ZyzTuanDuiMemberListAdapter.ViewHolder holder, final int position) {
        // 绑定数据
        final VolunteerMemberEntity itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getName()));

        if (itemData.getBirthday() == null) {
            holder.ageTv.setText("0");
        } else {
            holder.ageTv.setText("" + XUtils.date().getAge(XUtils.date().getDateByDateStr(itemData.getBirthday(), "yyyy-MM-dd")));
        }

        holder.allTimeTv.setText(XUtils.string().getNotNullValue(itemData.getDuration(), "0"));
        holder.thisYearNumTv.setText(XUtils.string().getNotNullValue(itemData.getThisYearDuration(), "0"));
        holder.lastYearNumTv.setText(XUtils.string().getNotNullValue(itemData.getLastYearDuration(), "0"));

        holder.phoneTv.setText(XUtils.string().getNotNullValue(itemData.getPhone()));
        holder.emailTv.setText(XUtils.string().getNotNullValue(itemData.getEmail(), " — "));

        Picasso.get().load(itemData.getPhoto())
                .error(R.drawable.icon_default_img)
                .placeholder(R.drawable.icon_default_img)
                .into(holder.imageView);

//        holder.rootView.setOnClickListener(v -> {
//            if (onItemClickListener != null) {
//                onItemClickListener.onClickListener(position, itemData);
//            }
//        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rootView;
        TextView titleTv, phoneTv, emailTv, allTimeTv, thisYearNumTv, lastYearNumTv, ageTv;
        RoundedImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            ageTv = itemView.findViewById(R.id.ageTv);

            allTimeTv = itemView.findViewById(R.id.allTimeTv);
            thisYearNumTv = itemView.findViewById(R.id.thisYearNumTv);
            lastYearNumTv = itemView.findViewById(R.id.lastYearNumTv);

            phoneTv = itemView.findViewById(R.id.phoneTv);
            emailTv = itemView.findViewById(R.id.emailTv);

            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}